<template>
	<view class="input-box">
		<view class="input-title">反馈问题</view>
		<view class="input-item">
			<uni-data-select v-model="FormData.FankuiFL" :localdata="array" placeholder="请选择反馈分类"></uni-data-select>
		</view>
	</view>
	<view class="input-box" style="display: block">
		<view class="input-title" style="margin-bottom: 22rpx">上传图片</view>
		<uni-file-picker
			:value="FormData.TupianMS"
			file-mediatype="image"
			limit="10"
			@success="success"
			sourceType="['album', 'camera']"
			@select="SelectImage($event, 'TupianMS')"
			@delete="deleteImage($event, 'TupianMS')"
		>
			<view class="image-add">
				<tui-icon name="plus" color="#979797" size="20" bold="true"></tui-icon>
			</view>
		</uni-file-picker>
	</view>
	<view class="input-box" style="display: block; border: 0">
		<view class="input-title" style="margin-bottom: 22rpx">补充说明</view>
		<textarea v-model="FormData.FankuiNR" placeholder="请输入反馈内容"></textarea>
	</view>
	<view class="add-btn" @tap="submit">提交</view>
</template>

<script>
import { uploadFile } from '@/utils/tool.js';
export default {
	data() {
		return {
			startTime:'',
			Islogin:this.Microi.IsLogin(),
			array: [],
			FormData: {
				FankuiFL: '',
				FankuiNR: '',
				TupianMS: [],
				IsDisabled: false
			}
		};
	},
	onLoad() {
		this.getFLarray();
	},
	onShow() {
		this.startTime = new Date();
	},
	onHide() {
		if (this.Islogin) {
			// 获取当前页面栈
			const pages = getCurrentPages();
			// 获取当前页面的对象
			const currentPage = pages[pages.length - 1];
			// 获取当前页面的路径
			const route = currentPage.route;
			// 获取当前页面的参数
			const options = currentPage.options;
			this.Microi.ApiEngine.Run('Add_liulanLog', {
				YemianCS: options,
				YemianDZ: route,
				LiulanSJC: new Date().getTime() - this.startTime.getTime()
			});
		}
	},
	onUnload() {
		if (this.Islogin) {
			// 获取当前页面栈
			const pages = getCurrentPages();
			// 获取当前页面的对象
			const currentPage = pages[pages.length - 1];
			// 获取当前页面的路径
			const route = currentPage.route;
			// 获取当前页面的参数
			const options = currentPage.options;
			this.Microi.ApiEngine.Run('Add_liulanLog', {
				YemianCS: options,
				YemianDZ: route,
				LiulanSJC: new Date().getTime() - this.startTime.getTime()
			});
		}
	},
	methods: {
		async getFLarray() {
			var self = this;
			var res = await self.Microi.ApiEngine.Run('customize_crud', {
				EngineKey: 'KEY000001'
			});
			this.array = res.Data;
		},
		async submit() {
			var self = this;
			if (self.IsDisabled) {
				self.Microi.Tips('请勿重复提交', false);
				return;
			}
			self.IsDisabled = true;
			var FormData = self.FormData;
			if (!FormData.FankuiFL) {
				self.Microi.Tips('请选择问题分类', false);
				self.IsDisabled = false;
				return;
			}
			if (!FormData.FankuiNR) {
				self.Microi.Tips('请输入反馈内容', false);
				self.IsDisabled = false;
				return;
			}
			FormData.TupianMS = JSON.stringify(FormData.TupianMS);
			var res = await self.Microi.FormEngine.AddFormData({
				FormEngineKey: 'diy_YijianFK',
				_RowModel: FormData
			});
			self.Microi.Tips('感谢您的反馈', true);
			setTimeout(function () {
				uni.navigateBack({
					delta: 1
				});
			}, 1000);
		},
		// 照片上传
		async SelectImage(e, name) {
			uni.showLoading({
				title: '上传照片中',
				mask: true
			});
			var array = e.tempFilePaths;
			array.forEach(async (item) => {
				var res = await this.uploadImage(item);
				this.FormData[name].push(res);
			});
			setTimeout(function () {
				uni.hideLoading();
			}, 1000);
		},
		deleteImage(e, name) {
			const index = this.FormData[name].findIndex((item) => item.Url == e.url);
			if (index > -1) {
				this.FormData[name].splice(index, 1);
			}

			console.log(this.FormData, 'this.FormData');
		},
		async uploadImage(Path) {
			console.log(Path, 'Path');
			if (Path.indexOf('https://minio.nbweixin.cn/public') == -1) {
				console.log('into', Path);
				var res = await this.Microi.Upload({
					Path: '/img', // 文件路径前缀
					Limit: false, // 是否上传到私有存储桶
					Preview: true, // 是否压缩图片
					_FilePath: Path
				});
				if (res.Code == 1) {
					var obj = {
						Path: res.Data.Path,
						Name: res.Data.Name,
						Size: res.Data.Size,
						CreateTime: res.Data.CreateTime,
						Id: res.Data.Id,
						url: this.GetServerPath(res.Data.Path),
						name: res.Data.Name,
						extname: res.Data.Path.split('.')[1]
					};
					return obj;
				}
			} else {
				let endIndex = Path.indexOf('https://minio.nbweixin.cn/public');
				// code = url.substring(0,endIndex)
				var Path1 = Path.substring(0, endIndex);
				return Path1;
			}
		},
		GetServerPath(Path) {
			if (!Path) {
				return '';
			}
			return this.Microi.FileServer + Path;
		}
	}
};
</script>

<style>
page {
	background-color: #fff;
}
.input-box {
	padding: 34rpx 30rpx;
	width: 690rpx;
	background: #ffffff;
	box-sizing: border-box;
	border-bottom: 1rpx solid #f3f5f9;
	display: flex;
	align-items: center;
	justify-content: space-between;
	margin: auto;

	font-weight: 500;
	font-size: 28rpx;
	color: #333333;
	line-height: 33rpx;
	font-style: normal;
	text-transform: none;
}
.image-add {
	width: 180rpx;
	height: 180rpx;
	background: #f4f5f6;
	border-radius: 16rpx 16rpx 16rpx 16rpx;
	text-align: center;
	line-height: 180rpx;
}
.input-title {
	font-weight: 500;
	font-size: 28rpx;
	color: #333333;
	line-height: 33rpx;
	font-style: normal;
	text-transform: none;
}
textarea {
	height: 334rpx;
	background: #f4f5f6;
	border-radius: 16rpx 16rpx 16rpx 16rpx;
	padding: 32rpx 36rpx;
	width: 100%;
	box-sizing: border-box;
}
.add-btn {
	width: 496rpx;
	height: 68rpx;
	background: #f4f5f6;
	border-radius: 80rpx 80rpx 80rpx 80rpx;
	line-height: 68rpx;
	margin: auto;
	margin-top: 280rpx;
	font-weight: 800;
	font-size: 32rpx;
	color: #999999;
	text-align: center;
	font-style: normal;
	text-transform: none;
}
</style>
